package com.hrm.domain.system.response;

import com.hrm.domain.system.Permission;
import com.hrm.domain.system.Role;
import com.hrm.domain.system.User;
import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;
import java.util.*;

@Getter
@Setter
public class ProfileResult implements Serializable {
    private String userId;
    private String mobile;
    private String username;
    private String company;
    private String companyId;

    /**
     * 角色数据,权限代码集合
     */
    private Map<String, Object> roles;

    /**
     *
     */
    public ProfileResult(User user, List<Permission> list) {
        this.userId = user.getId();
        this.mobile = user.getMobile();
        this.username = user.getUsername();
        this.company = user.getCompanyName();
        this.companyId = user.getCompanyId();
        //角色数据
        Set<String> menus = new HashSet<>();
        Set<String> points = new HashSet<>();
        Set<String> apis = new HashSet<>();
        Map<String, Object> rolesMap = new HashMap<>();
        for (Permission perm : list) {
            String code = perm.getCode();
            if (perm.getType() == 1) {
                menus.add(code);
            } else if (perm.getType() == 2) {
                points.add(code);
            } else if (perm.getType() == 3) {
                apis.add(code);
            }
        }
        rolesMap.put("menus", menus);
        rolesMap.put("points", points);
        rolesMap.put("apis", apis);
        this.roles = rolesMap;
    }

    public ProfileResult(User user) {
        this.mobile = user.getMobile();
        this.username = user.getUsername();
        this.company = user.getCompanyName();
        this.companyId = user.getCompanyId();
        //角色数据
        Set<String> menus = new HashSet<>();
        Set<String> points = new HashSet<>();
        Set<String> apis = new HashSet<>();
        Map<String, Object> rolesMap = new HashMap<>();
        for (Role role : user.getRoles()) {
            for (Permission perm : role.getPermissions()) {
                String code = perm.getCode();
                if (perm.getType() == 1) {
                    menus.add(code);
                } else if (perm.getType() == 2) {
                    points.add(code);
                } else if (perm.getType() == 3) {
                    apis.add(code);
                }
            }
        }
        rolesMap.put("menus", menus);
        rolesMap.put("points", points);
        rolesMap.put("apis", apis);
        this.roles = rolesMap;
    }
}
